﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.User;
using CE.Provider;
using CE.Global;

public partial class car_Insurance_ViewProviderWorkShop : MySessionBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Customer) && Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Provider))
            {
                Response.Redirect("login.aspx");
            }
        }
        else
        {
            Response.Redirect("login.aspx");
        }

        if (!Page.IsPostBack)
        {
            if (Request.Params["comid"] != null)
            {
                ShowProviderWorkShopList(Convert.ToInt64(Request.Params["comid"].ToString()));
            }
            LogUserViewedHistory();
        }

    }

    private void LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        _userviewdb.LogViewedHistory("CI", Request, "Car Insurance view provider work shop.");
    }

    private void ShowProviderWorkShopList(Int64 ProviderCompanyId)
    {
        ProviderWorkShopList _providerwslist = new ProviderWorkShopList();
        ProviderWorkShopDB _providerwsdb = new ProviderWorkShopDB();
        Company _com = new Company();
        CompanyDB _comdb = new CompanyDB();
        _com = _comdb.GetCompany(ProviderCompanyId);
        
        CompanyLogo _comLogo = new CompanyLogo();
        _comLogo = _comdb.GetCompanyLogo(ProviderCompanyId);
        if(_comLogo != null)
            imgCompanyLogo.ImageUrl = _comLogo.CompanyLogoServerUrl;

        if (_com != null)
        {
            lblCompanyName.Text = _com.CompanyName;
            Page.Title = lblCompanyName.Text + "'s Workshop(s)";
        }

        _providerwslist = _providerwsdb.GetProviderWorkShopList(ProviderCompanyId);

        DataTable _dtworkshop = new DataTable("WorkShopTable");
        _dtworkshop.Columns.Add("Work Shop Name");
        _dtworkshop.Columns.Add("Address");
        _dtworkshop.Columns.Add("Country");
        _dtworkshop.Columns.Add("PostCode");
        _dtworkshop.Columns.Add("Zone");
        _dtworkshop.Columns.Add("Contact No.");

        if (_providerwslist != null)
        {
            int _pcount = _providerwslist.Count;
            for (int i = 0; i < _pcount; i++)
            {
                DataRow _drnew = _dtworkshop.NewRow();
                ProviderWorkShop _wsnew = _providerwslist[i];
                string _address = "";
                _drnew[0] = _wsnew.WSName;
                
                if (_wsnew.BlockNo != string.Empty)
                {
                    _address += "Blk " + _wsnew.BlockNo + ", ";
                }

                if (_wsnew.AreaName != string.Empty)
                {
                    _address += _wsnew.AreaName + ", " ;
                }

                string lvlNo = "";
                string rmNo = "";

                if (_wsnew.LevelNo != string.Empty)
                {
                    lvlNo = _wsnew.LevelNo;
                }

                if (_wsnew.RoomNo != string.Empty)
                {
                    rmNo += _wsnew.RoomNo;
                }

                if (lvlNo != string.Empty && rmNo != string.Empty)
                {
                    _address += "#" + lvlNo + "-" + rmNo + ", ";
                }
                else if (lvlNo != string.Empty && rmNo == string.Empty)
                {
                    _address += "Lvl No. " + lvlNo + ", ";
                }
                else if (lvlNo == string.Empty && rmNo != string.Empty)
                {
                    _address += "Rm No. " + rmNo + ", ";
                }

                if (_wsnew.BuildingName != string.Empty)
                {
                    _address += _wsnew.BuildingName;
                }
                else
                {
                    _address = _address.Substring(0, _address.Length - 2);
                }

                _drnew[1] = _address;

                string _postcode = "";
                string _country = "";

                if (_wsnew.Country != null)
                {
                    _country = _wsnew.Country + " ";
                }
                else
                {
                    _country = "Singapore ";
                }

                _drnew[2] = _country;

                if (_wsnew.PostCode != null)
                {
                    _postcode += _wsnew.PostCode;
                }

                _drnew[3] = _postcode;
                _drnew[4] = _wsnew.ZoneName;
                _drnew[5] = _wsnew.Info1 ;
                _dtworkshop.Rows.Add(_drnew);
            }

            grvWorkShop.DataSource = _dtworkshop;
            grvWorkShop.DataBind();
        }
    }
}
